﻿using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using MainGameModule.Models;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using YourWorld.Common;
using YourWorld.Common.GreenWorldEntitiesService;
using MainGameModule.Views;

namespace MainGameModule.ViewModels
{
    public class QuizViewModel : ViewModelBase
    {
        private IRegionManager regionManager;
        private IUnityContainer container;

        public int numberOfQuestions = 10;

        private List<Question> questions;
        private Question currentQuestion;
        private int currentQuestionNumber;
        private string validityMessage;
        private int numberOfCorrectAnswers;
        private int earnedExperience;

        public ICommand CloseQuizCommand
        {
            get;
            private set;
        }

        public ICommand ShowAnswerCommand
        { get; set; }

        public ICommand NextQuestionCommand
        { get; set; }

        public ICommand FinishCommand
        { get; set; }

        public string buttonText;

        public string ButtonText
        {
            get
            {
                return this.buttonText;
            }
            set
            {
                if (this.buttonText != value)
                {
                    this.buttonText = value;
                    this.NotifyPropertyChanged("ButtonText");
                }
            }
        }

        public string ValidityMessage
        {
            get
            {
                return this.validityMessage;
            }
            set
            {
                this.validityMessage = value;
                this.NotifyPropertyChanged("ValidityMessage");
            }
        }

        public List<Question> Questions
        {
            get
            {
                return this.questions;
            }
            set
            {
                if (this.questions != value)
                {
                    this.questions = value;
                    this.NotifyPropertyChanged("Questions");
                }
            }
        }

        public Question CurrentQuestion
        {
            get
            {
                return this.currentQuestion;
            }
            set
            {
                if (this.currentQuestion != value)
                {
                    this.currentQuestion = value;
                    this.NotifyPropertyChanged("CurrentQuestion");
                }
            }
        }

        public int NumberOfQuestions
        {
            get
            {
                return this.numberOfQuestions;
            }
            set
            {
                if (this.numberOfQuestions != value)
                {
                    this.numberOfQuestions = value;
                    this.NotifyPropertyChanged("NumberOfQuestions");
                }
            }
        }

        public int CurrentQuestionNumber
        {
            get
            {
                return this.currentQuestionNumber;
            }
            set
            {
                if (this.currentQuestionNumber != value)
                {
                    this.currentQuestionNumber = value;
                    this.NotifyPropertyChanged("CurrentQuestionNumber");
                }
            }
        }

        public int NumberOfCorrectAnswers
        {
            get
            {
                return this.numberOfCorrectAnswers;
            }
            set
            {
                if (this.numberOfCorrectAnswers != value)
                {
                    this.numberOfCorrectAnswers = value;
                    this.NotifyPropertyChanged("NumberOfCorrectAnswers");
                }
            }
        }

        public int EarnedExperience
        {
            get
            {
                return this.earnedExperience;
            }
            set
            {
                if (this.earnedExperience != value)
                {
                    this.earnedExperience = value;
                    this.NotifyPropertyChanged("EarnedExperience");
                }
            }
        }

        public QuizViewModel(IRegionManager regionManager, IUnityContainer container)
        {
            this.regionManager = regionManager;
            this.container = container;

            this.CurrentQuestionNumber = 1;
            this.NumberOfCorrectAnswers = 0;

            this.InitializeQuestions();

            this.ShowAnswerCommand = new DelegateCommand<string>(this.ShowAnswer);
            this.NextQuestionCommand = new DelegateCommand<string>(this.NextQuestion);
            this.FinishCommand = new DelegateCommand<string>(this.Finish);
            this.CloseQuizCommand = new DelegateCommand<string>(this.CloseQuizView);
        }

        private void InitializeQuestions()
        {
            this.questions = new List<Question>();

            if (QuestionsRepository.Instance.Questions.Count <= this.numberOfQuestions)
            {
                this.NumberOfQuestions = QuestionsRepository.Instance.Questions.Count;
                foreach (Question q in QuestionsRepository.Instance.Questions)
                {
                    Question question = new Question()
                    {
                        Text = q.Text,
                        A = q.A,
                        B = q.B,
                        C = q.C,
                        D = q.D,
                        CorrectAnswer = q.CorrectAnswer,
                        Explanation = q.Explanation,
                    };

                    this.questions.Add(question);
                }
            }
            else
            {
                // shuffle
                //Random rnd = new Random();
                //for (int i = 1; i < QuestionsRepository.Instance.Questions.Count; i++) {
                //  int pos = rnd.Next(i + 1);
                //  var x = QuestionsRepository.Instance.Questions[i];
                //  QuestionsRepository.Instance.Questions[i] = QuestionsRepository.Instance.Questions[pos];
                //  QuestionsRepository.Instance.Questions[pos] = x;
                //}

                foreach (Question q in QuestionsRepository.Instance.Questions.Take(this.NumberOfQuestions))
                {
                    Question question = new Question()
                    {
                        Text = q.Text,
                        A = q.A,
                        B = q.B,
                        C = q.C,
                        D = q.D,
                        CorrectAnswer = q.CorrectAnswer,
                        Explanation = q.Explanation,
                    };

                    this.questions.Add(question);
                }
            }

            this.CurrentQuestion = this.Questions[0];
        }

        public void Finish(string notUsed)
        {
            // todo: set EarnedExperience
            // todo: earn experience
        }

        public void ShowAnswer(string notUsed)
        {

        }

        public void NextQuestion(string notUsed)
        {
            // there is a next question
            if (this.CurrentQuestionNumber != this.Questions.Count)
            {
                this.CurrentQuestion = this.Questions[this.CurrentQuestionNumber];
                this.CurrentQuestionNumber++;
            }
        }

        private void CloseQuizView(string notUsed)
        {
            this.regionManager.Regions[RegionNames.GamePopUpRegion].Deactivate(this.container.Resolve<QuizView>());
        }
    }
}
